<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        fieldset, img, input, button {
            border: none;
            padding: 0;
            margin: 0;
            outline-style: none;
        }

        ul, ol {
            list-style: none;
            margin: 0px;
            padding: 0px;
        }

        #box {
            width: 405px;
            margin: 200px auto;
            position: relative;
        }

        #txtSearch {
            float: left;
            width: 300px;
            height: 32px;
            padding-left: 4px;
            border: 1px solid #b6b6b6;
            border-right: 0;
        }

        #btnSearch {
            float: left;
            width: 100px;
            height: 34px;
            font: 400 14px/34px "microsoft yahei";
            color: white;
            background: #3385ff;
            cursor: pointer;
        }

        #btnSearch:hover {
            background: #317ef3;
        }

        #pop {
            width: 303px;
            border: 1px solid #ccc;
            padding: 0px;
            position: absolute;
            top: 34px;
        }

        #pop ul li {
            padding-left: 5px;
        }

        /*#pop ul li:hover {
            background-color: #CCC;
        }*/
    </style>
</head>
<body>
<div id="box">
    <input type="text" id="txtSearch">
    <input type="button" value="百度一下" id="btnSearch">
</div>
<script src="common.js"></script>
<script>
    var bg = null;
    var currentLi = null;
    var box = document.getElementById("box");
    var txt = document.getElementById("txtSearch");
    var datas = ["a", "abc", "abbbb", "abxxx", "xyz", "abcdef", "abzzzzzz"];
    txt.onkeyup = txtKeyUp;
    function txtKeyUp() {
        var val = this.value;
        var filterArr = [];
        for (var i = 0; i < datas.length; i++) {
            var data = datas[i];
            if (data.indexOf(val) === 0) {
                filterArr.push(data);
            }
        }
        var popDiv = document.getElementById("pop");
        if (popDiv) {
            box.removeChild(popDiv);
        }
        if (val.length === 0) {
            return;
        }
        if (filterArr.length === 0) {
            return;
        }
        var popDiv = document.createElement("div");
        box.appendChild(popDiv);
        popDiv.id = "pop";
        var ul = document.createElement("ul");
        ul.id = "popList";
        popDiv.appendChild(ul);
        for (var i = 0; i < filterArr.length; i++) {
            var item = filterArr[i];
            var li = document.createElement("li");
            ul.appendChild(li);
            li.innerText = item;
            //1. 鼠标经过当前li高亮
            li.onmouseover = function () {
                bg = this.style.backgroundColor;
                currentLi = this;
                var lis = ul.children;
                for (var i = 0; i < lis.length; i++) {
                    lis[i].style.backgroundColor = "";
                }
                this.style.backgroundColor = "#ccc";
            }
            li.onmouseout = function () {
                this.style.backgroundColor = bg;
            }
            //2. 点击li 将当前li中的文本放到文本框中
            li.onclick = function () {
                txt.value = this.innerText;
                box.removeChild(popDiv);
                currentLi = null;
            }
        }
    }
    //3. 通过键盘控制
    document.onkeyup = function (e) {
        //通过上下键控制选择
        if (e.keyCode === 40 || e.keyCode === 38) {
            txt.onkeyup = null;
            txt.blur();
            var ul = document.getElementById("popList");
            var lis = ul.children;
            for (var i = 0; i < lis.length; i++) {
                lis[i].style.backgroundColor = "";
            }
            if (currentLi) {
                if (e.keyCode === 40 && currentLi !== lis[lis.length - 1]) {
                    currentLi = currentLi.nextElementSibling;
                }
            } else {
                currentLi = lis[0];
            }
            if (e.keyCode === 38 && currentLi !== lis[0]) {
                currentLi = currentLi.previousElementSibling;
            }
            currentLi.style.backgroundColor = "#ccc";
        }
        //按回车键选中当前li
        if (e.keyCode === 13) {
            txt.value = currentLi.innerText;
            var popDiv = document.getElementById("pop");
            box.removeChild(popDiv);
            currentLi = null;
        }
    };
    //文本框获取焦点后恢复键盘弹起的事件
    txt.onfocus = function () {
        txt.onkeyup = txtKeyUp;
        currentLi = null;
    };
</script>
</body>
</html>